﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.BusinessLayer;
using FinanSoft.Entities;
using FinanSoft.Utilities;
using System.Xml;

namespace FinanSoft
{
    public partial class frmCompañia : Form
    {
        private bool _isModified;
        private String[] arryDat = new String[4];
        public frmCompañia(bool isModified)
        {
            _isModified = isModified;
            InitializeComponent();
            fillPercentages();
            fillCompany();
        }

        private void fillPercentages()
        {
            for (int percentage = 0; percentage <= 100; percentage++)
            {
                dividendCmbBox.Items.Add(percentage.ToString());
                taxCmbBox.Items.Add(percentage.ToString());
            }
            dividendCmbBox.SelectedIndex = 0;
            dividendCmbBox.SelectedIndex = 0;
        }

        private void fillCompany() 
        {
            if (_isModified)
            {
                Company company = ReportManager.GetInstance().LoadedCompany;
                nameBox.Text = company.Name;
                phoneBox.Text = company.Phone;
                faxBox.Text = company.Fax;
                idBox.Text = company.LegalId;
                idBox.Enabled = false;
                dividendCmbBox.SelectedIndex = Convert.ToInt32(company.DividendPercentage*100);
                taxCmbBox.SelectedIndex = Convert.ToInt32(company.TaxPercentage*100);
            }
            else 
            {
                btnAceptar.Text = "Siguiente";
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Close();

        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (validateInformation())
            {
                if (_isModified)
                {
                    String originalName = ReportManager.GetInstance().LoadedCompany.Name;
                    bool wasModified = ReportManager.GetInstance().ModifyCompany(idBox.Text, nameBox.Text, phoneBox.Text, faxBox.Text, dividendCmbBox.SelectedIndex*0.01, taxCmbBox.SelectedIndex * 0.01);
                    if (wasModified)
                    {
                        XmlDocument xDoc = new XmlDocument();
                        xDoc.Load(Constants.DIRECTORY_COMPANIES);
                        XmlNodeList empresas = xDoc.GetElementsByTagName(Constants.TAG_COMPANY);
                        foreach (XmlNode empresa in empresas)
                            if (empresa.Attributes[Constants.TAG_COMPANY_NAME].Value.Equals(originalName))
                                empresa.Attributes[Constants.TAG_COMPANY_NAME].Value = nameBox.Text;
                        ReportManager.GetInstance().LoadedCompany.Name = nameBox.Text;
                        xDoc.Save(Constants.DIRECTORY_COMPANIES);
                        MessageBox.Show(MessageConstants.COMPANY_EDITED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                        MessageBox.Show(ReportManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Company newCompany = new Company(nameBox.Text, idBox.Text, phoneBox.Text, faxBox.Text, ReportManager.GetInstance().LoadedCompany.Logo, dividendCmbBox.SelectedIndex*0.01, taxCmbBox.SelectedIndex*0.01);
                    frmCrearEmpresa frmNuevaEmpresa = new frmCrearEmpresa(newCompany);
                    frmNuevaEmpresa.ShowDialog();
                    Close();
                }
            }
            else
                MessageBox.Show(MessageConstants.COMPANY_INFORMATION_MISSING, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }


        /// <summary>
        /// Valida que no haya campos vacíos
        /// </summary>
        /// <returns>Devuelve verdadero si la información es válida</returns>
        private bool validateInformation()
        {
            return (nameBox.Text.Trim().Length > 0 && idBox.Text.Trim().Length > 0 && phoneBox.Text.Trim().Length > 0 && faxBox.Text.Trim().Length > 0);
        }

        private void btnCancelar_Click_1(object sender, EventArgs e)
        {
            Close();
        }
    }
}
